import { triplitClient } from "@renderer/client";
import type { FullQueries } from "../types";

/**
 * Thread-MCP Server query definitions for standardized database access
 */
export const threadMcpServersQueries: FullQueries<"threadMcpServers"> & {
  /** Get associations by thread ID */
  byThreadId: (
    threadId: string,
  ) => ReturnType<typeof triplitClient.query<"threadMcpServers">>;
  /** Get enabled associations by thread ID */
  enabledByThreadId: (
    threadId: string,
  ) => ReturnType<typeof triplitClient.query<"threadMcpServers">>;
  /** Get associations by MCP server ID */
  byMcpServerId: (
    mcpServerId: string,
  ) => ReturnType<typeof triplitClient.query<"threadMcpServers">>;
  /** Get associations with MCP server details by thread ID */
  withServersByThreadId: (
    threadId: string,
  ) => ReturnType<typeof triplitClient.query<"threadMcpServers">>;
  /** Get enabled associations with MCP server details by thread ID */
  enabledWithServersByThreadId: (
    threadId: string,
  ) => ReturnType<typeof triplitClient.query<"threadMcpServers">>;
  /** Get associations with thread details by MCP server ID */
  withThreadsByMcpServerId: (
    mcpServerId: string,
  ) => ReturnType<typeof triplitClient.query<"threadMcpServers">>;
  /** Check if a specific server is associated with a thread */
  byThreadIdAndServerId: (
    threadId: string,
    mcpServerId: string,
  ) => ReturnType<typeof triplitClient.query<"threadMcpServers">>;
  /** Check if a specific server is enabled for a thread */
  enabledByThreadIdAndServerId: (
    threadId: string,
    mcpServerId: string,
  ) => ReturnType<typeof triplitClient.query<"threadMcpServers">>;
} = {
  /**
   * Get all thread-MCP server associations
   */
  all: () => triplitClient.query("threadMcpServers"),

  /**
   * Get enabled thread-MCP server associations
   */
  enabled: () =>
    triplitClient.query("threadMcpServers").Where("enabled", "=", true),

  /**
   * Get ordered thread-MCP server associations
   */
  ordered: () => triplitClient.query("threadMcpServers").Order("order", "ASC"),

  /**
   * Get thread-MCP server association by ID
   */
  byId: (id: string) => triplitClient.query("threadMcpServers").Id(id),

  /**
   * Get associations by thread ID
   */
  byThreadId: (threadId: string) =>
    triplitClient
      .query("threadMcpServers")
      .Where("threadId", "=", threadId)
      .Order("order", "ASC"),

  /**
   * Get enabled associations by thread ID
   */
  enabledByThreadId: (threadId: string) =>
    triplitClient
      .query("threadMcpServers")
      .Where("threadId", "=", threadId)
      .Where("enabled", "=", true)
      .Order("order", "ASC"),

  /**
   * Get associations by MCP server ID
   */
  byMcpServerId: (mcpServerId: string) =>
    triplitClient
      .query("threadMcpServers")
      .Where("mcpServerId", "=", mcpServerId),

  /**
   * Get associations with MCP server details by thread ID
   */
  withServersByThreadId: (threadId: string) =>
    triplitClient
      .query("threadMcpServers")
      .Where("threadId", "=", threadId)
      .Include("mcpServer")
      .Order("order", "ASC"),

  /**
   * Get enabled associations with MCP server details by thread ID
   */
  enabledWithServersByThreadId: (threadId: string) =>
    triplitClient
      .query("threadMcpServers")
      .Where("threadId", "=", threadId)
      .Where("enabled", "=", true)
      .Include("mcpServer")
      .Order("order", "ASC"),

  /**
   * Get associations with thread details by MCP server ID
   */
  withThreadsByMcpServerId: (mcpServerId: string) =>
    triplitClient
      .query("threadMcpServers")
      .Where("mcpServerId", "=", mcpServerId)
      .Include("thread"),

  /**
   * Check if a specific server is associated with a thread
   */
  byThreadIdAndServerId: (threadId: string, mcpServerId: string) =>
    triplitClient
      .query("threadMcpServers")
      .Where("threadId", "=", threadId)
      .Where("mcpServerId", "=", mcpServerId),

  /**
   * Check if a specific server is enabled for a thread
   */
  enabledByThreadIdAndServerId: (threadId: string, mcpServerId: string) =>
    triplitClient
      .query("threadMcpServers")
      .Where("threadId", "=", threadId)
      .Where("mcpServerId", "=", mcpServerId)
      .Where("enabled", "=", true),
};
